import { CaptchaService } from '@midwayjs/captcha';
import { httpError, Inject, Provide } from '@midwayjs/core';

@Provide()
export class CaptchaCodeService {

    @Inject()
    captchaService: CaptchaService;

    async getCaptcha() {
        // const captcha = await this.captchaService.image({ width: 100, height: 40 })
        const captcha = await this.captchaService.formula({ noise: 1 })

        return captcha;
    }

    async checkCaptcha(id, code) {
        const passed: boolean = await this.captchaService.check(id, code)
        if (!passed) {
            throw new httpError.BadGatewayError('验证码错误!')
        }
        return passed;
    }
} 